package com.skyline.energy.datasource;

import java.util.List;

import javax.sql.DataSource;

import com.skyline.energy.dataaccess.jdbc.SqlExecuteContext;
import com.skyline.energy.dataaccess.jdbc.SqlExecuteContextHolder;
import com.skyline.energy.dataaccess.jdbc.SqlType;

public class MasterSlaverDataSource extends DistributeDataSource {
	private List<MasterDataSource> masters;
	private List<DataSource> slavers;
	
	public List<MasterDataSource> getMasters() {
		return masters;
	}

	public List<DataSource> getSlavers() {
		return slavers;
	}

	@Override
	public DataSource getDataSource() {
		DataSource txDataSource = getTxDataSource();
		if(txDataSource == null) {
			//TODO
			return null;
		}
		
		if(canUseOld(txDataSource)) {
			return txDataSource;
		} else {
			//TODO
			return null;
		}
	}
	
	private boolean canUseOld(DataSource txDataSource) {
		SqlExecuteContext sqlContext = SqlExecuteContextHolder.getContext();
		if(sqlContext.getSqlType() == SqlType.UPDATE && txDataSource instanceof MasterDataSource) {
			return true;
		}
		return false;
	}
}
